/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package cn.laike.modules.dev.utils;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

import cn.laike.common.utils.HttpRequest;
import cn.laike.common.utils.R;
import cn.laike.common.utils.RedisUtils;
import cn.laike.modules.dev.entity.ConfigEntity;
import cn.laike.modules.dev.service.ConfigService;
import net.sf.json.JSONObject;

/**
 * 接口配置工具类
 * 
 * @author Administrator
 *
 */
@Service
public class DevCacheUtils implements ApplicationContextAware {

	private static RedisUtils redisUtils;
	
    private static ConfigService configService;
	
    /**
     * 获取小程序配置
     * @param uid
     * @return
     */
    public static ConfigEntity getConfig(Long uid) {
		String key = "config_"+uid;
		Object result = redisUtils.getObject(key);
		if (result != null) {
			return (ConfigEntity)result;
		}else{
			ConfigEntity config = configService.getByUid(uid);
			if(config != null && config.getId() != null && config.getId() > 0){
				redisUtils.setObject(key, config);
			}
			return config;
		}
	}
    
    
	 /**
     * 判断key是否存在
     * @param key 键
     * @return true 存在 false不存在
     */
    public boolean hasKey(String key) {
        return redisUtils.hasKey(key);
    }
    /**
     * 删除缓存
     * @param key 可以传一个值 或多个
     */
	public static void del(String... key) {
		redisUtils.del(key);
	}
	

	


	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		redisUtils = (RedisUtils) applicationContext.getBean("redisUtils");
		configService = (ConfigService) applicationContext.getBean("configService");
		
	}
}
